package com.prp.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import com.prp.bo.MessageBoardBO;
import com.prp.bo.SNSConstants;
import com.prp.bo.User;
import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.IKeywordsLibDAO;
import com.prp.db.idao.INeighbourRelationshipDAO;
import com.prp.db.idao.IRecommendationEntityDAO;
import com.prp.db.idao.ISearchRecordDAO;
import com.prp.db.idao.ISubjectCategoryDAO;
import com.prp.db.idao.ISubjectChildCategoryDAO;
import com.prp.db.idao.ISubjectRecommendationDAO;
import com.prp.db.idao.ITempKeywordDAO;
import com.prp.db.idao.IUserInformationDAO;
import com.prp.db.po.KeywordsLib;
import com.prp.db.po.Msgboard;
import com.prp.db.po.NeighbourRelationship;
import com.prp.db.po.NeighbourRelationshipId;
import com.prp.db.po.RecommendationEntity;
import com.prp.db.po.SearchRecord;
import com.prp.db.po.SubjectCategory;
import com.prp.db.po.SubjectChildCategory;
import com.prp.db.po.SubjectRecommendation;
import com.prp.db.po.TempKeyword;
import com.prp.db.po.UserInformation;
import com.prp.gwt.client.vo.MessageList;
import com.prp.gwt.server.PHPServiceImpl;

public class CreateDBData {
	private static Random r = new Random(System.currentTimeMillis());
	private static Transaction tx;

	public static void main(String[] Args) {
		/**
		 * the following methods should be used by order, otherwise error will
		 * be caused because of the constraints between tables
		 */
		ArrayList<Msgboard> list =  MessageBoardBO.findByUnchecked(204);
		MessageList msgList = new MessageList();
		for (Msgboard msg : list)
			msgList.add(PHPServiceImpl.parseMessage(msg));
		msgList.setCurrentUser(User.parse(User.getUser(204)));
		System.out.println(msgList.size());
		// createUserInformation();
		// createNeighbourRelationship();
		// createSubjectCategory();
		// createSubjectChildCategory();
		// createSearchRecord();
		// createRecommendationEntity();
		// createSubjectRecommendationAndKeywordLib();
		// createTempKeyword();
	}

	public static void createUser(String email, String username, String password) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = new UserInformation();
			user.setEmail(email);
			user.setUserRegName(username);
			user.setPassword(password);
			user.setGender(SNSConstants.User_Male);
			user.setIdentity(1);
			//user.setUserClassId(1);
			user.setUserGradeId(1);
			userDAO.save(user);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}

	public static void createNeighbourRelationship() {
		DAOFactory factory = DAOFactory.getDAOFactory();
		INeighbourRelationshipDAO nrDAO = factory.getNeighbourRelationshipDAO();
		NeighbourRelationship nr = null;
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		List<UserInformation> list = userDAO.findAll();
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < list.size(); j++) {
				boolean f = false;
				if (i < list.size() / 5) {
					if (r.nextInt(100) < 35) {
						f = true;
					}
				} else {
					if (r.nextInt(100) < 10) {
						f = true;
					}
				}
				if (f) {
					try {
						tx = HibernateSessionFactory.getSession()
								.beginTransaction();
						nr = new NeighbourRelationship();
						nr.setId(new NeighbourRelationshipId(list.get(i)
								.getUserId(), list.get(j).getUserId()));
						nr.setBuildup(f);
						nrDAO.save(nr);
						tx.commit();
					} catch (Exception e) {
						if (tx != null)
							tx.rollback();
					}
				}
			}
		}
		// for (int i = 0; i < 1000; i++) {
		// try {
		// tx = HibernateSessionFactory.getSession().beginTransaction();
		// nr = new NeighbourRelationship();
		// UserInformation user1 = null;
		// // get user1
		// while (user1 == null)
		// user1 = (UserInformation) userDAO.findById(r.nextInt(200));
		// UserInformation user2 = null;
		// // get user2
		// while (true) {
		// user2 = (UserInformation) userDAO.findById(r.nextInt(200));
		// if (user2 == null || user2.getUserId() == user1.getUserId())
		// continue;
		// else {
		// List list = nrDAO.findByTwoUser(user1.getUserId(),
		// user2.getUserId());
		// if (list.size() != 0)
		// continue;
		// list = nrDAO.findByTwoUser(user2.getUserId(), user1
		// .getUserId());
		// if (list.size() == 0)
		// break;
		// }
		// }
		// // set neighbour relationship
		// nr.setFromId(user1.getUserId());
		// nr.setToId(user2.getUserId());
		// nr.setBuildup(r.nextInt(100) < 80 ? true : false); // set the
		// // probability
		// // of being
		// // neighbours
		// nrDAO.save(nr);
		// tx.commit();
		// } catch (Exception e) {
		// if (tx != null)
		// tx.rollback();
		// e.printStackTrace();
		// }
		// }
	}

	public static void createUserInformation() {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		UserInformation userInfo;
		List list;
		long l;
		for (int i = 0; i < 200; i++) {
			try {
				tx = HibernateSessionFactory.getSession().beginTransaction();
				userInfo = new UserInformation();
				userInfo.setPassword("");
				//userInfo.setUserClassId(1);
				//userInfo.setUserGradeId(1);
				userInfo.setAge(r.nextInt(100));
				userInfo.setGender(r.nextBoolean());
				userInfo.setIdentity(0);
				do {
					l = r.nextInt(100000);
					list = userDAO.findByEmail(l + "@hotmail.com");
				} while (list.size() != 0);
				userInfo.setEmail(l + "@hotmail.com");
				userInfo.setUserRegName(String.valueOf(l));
				//userInfo.setCommunityId(r.nextInt(5) + 1);
				userInfo.setActivated(true);
				userDAO.save(userInfo);
				tx.commit();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				tx.rollback();
				e.printStackTrace();
			}
		}
	}

	public static void createSubjectCategory(){
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCategoryDAO dao = factory.getSubjectCategoryDAO();
		SubjectCategory sc;
		for (int i = 0; i < 5; i++) {
			try {
				tx = HibernateSessionFactory.getSession().beginTransaction();
				sc = new SubjectCategory();
				sc.setCategoryName("category" + r.nextInt(10000));
				sc.setWaitingTime(1);
				sc.setClickFreq(r.nextInt(300));
				sc.setReplyNum(sc.getClickFreq()/5);
				dao.save(sc);
				tx.commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				if (tx != null)
					tx.rollback();
				e.printStackTrace();
			}
		}
	}
	
	public static void createSubjectChildCategory() {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectChildCategoryDAO dao = factory.getSubjectChildCategoryDAO();
		SubjectChildCategory sc;
		for (int i = 0; i < 50; i++) {
			try {
				tx = HibernateSessionFactory.getSession().beginTransaction();
				sc = new SubjectChildCategory();
				sc.setCategroyId(r.nextInt(5) + 1);
				sc.setChildCategoryName("childCategory" + r.nextInt(10000));
				dao.save(sc);
				tx.commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				if (tx != null)
					tx.rollback();
				e.printStackTrace();
			}
		}
	}

	public static void createSearchRecord() {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISearchRecordDAO dao = factory.getSearchRecordDAO();
		SearchRecord sr;
		for (int i = 1; i <= 200; i++) { // 200 is the number of user
			for (int j = 1; j <= 10; j++) {
				try {
					sr = new SearchRecord();
					sr.setCategory(r.nextInt(50) + 1);
					sr.setSearchUserId(i);
					sr.setSearchFreq(r.nextInt(100) + 1);
					tx = HibernateSessionFactory.getSession()
							.beginTransaction();
					dao.save(sr);
					tx.commit();
				} catch (HibernateException e) {
					if (tx != null)
						tx.commit();
					e.printStackTrace();
				}
			}
		}
	}

	public static void createRecommendationEntity() {
		IRecommendationEntityDAO dao = DAOFactory.getDAOFactory()
				.getRecommendationEntityDAO();
		Transaction tx = null;
		RecommendationEntity re = null;
		int tableSize = 500;
		for (int i = 0; i < tableSize; i++) {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			re = new RecommendationEntity();
			re.setChildCategoryId(r.nextInt(50) + 1);
			re.setEntityName(Long.toString(r.nextInt(999999)));
			re.setClickFreq(r.nextInt(200));
			re.setWaitingTime(r.nextInt(10000));
			re.setReplyNum(r.nextInt(100));
			dao.save(re);
			tx.commit();
		}
	}

	public static void createSubjectRecommendationAndKeywordLib() {
		ISubjectRecommendationDAO srdao = DAOFactory.getDAOFactory()
				.getSubjectRecommendationDAO();
		IKeywordsLibDAO kldao = DAOFactory.getDAOFactory().getKeywordsLibDAO();
		Transaction tx = null;
		SubjectRecommendation sr = null;
		//KeywordsLib kl = null;
		int tableSize = 500;
		for (int i = 0; i < tableSize; i++) {
			String title = Integer.toString(r.nextInt(99));
			sr = new SubjectRecommendation(r.nextInt(50) + 1, "subject" + title
					+ "title", r.nextInt(200) + 1, "content",
					r.nextInt(500) + 1, r.nextInt(200), r.nextInt(100000), r
							.nextInt(100), new java.util.Date());
			//kl = new KeywordsLib(title, r.nextInt(50) + 1);
			try {
				tx = HibernateSessionFactory.getSession().beginTransaction();
				srdao.save(sr);
				//kldao.save(kl);
				tx.commit();
			} catch (Exception e) {
				if (tx != null)
					tx.rollback();
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void createTempKeyword() {
		ITempKeywordDAO dao = DAOFactory.getDAOFactory().getTempKeywordDAO();
		TempKeyword tk = null;
		int tableSize = 100;
		for (int i = 0; i < tableSize; i++) {
			tk = new TempKeyword(Integer.toString(i + 100));
			try {
				tx = HibernateSessionFactory.getSession().beginTransaction();
				dao.save(tk);
				tx.commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				if (tx != null)
					tx.rollback();
				e.printStackTrace();
			}
		}
	}
}
